<?php
/**
 * Class MenuModule
 * 数据表 id pid name level icon/url
 */
class MenuModule extends SiteMode {
    /** @return LabelModule */
    public static function init() {
        return parent::init();
    }

    //添加类别
    public function add($name,$pid=0,$point=0,$level=null,$icon=null,$url=null){
        $data['name']=$name;
        $data['pid']=(int)$pid;
        $data['point']=(int)$point;
        $data['level']=$level;
        $data['icon']=$icon;
        $data['url']=$url;
        $data['site_id']=$this->siteId;
        if($this->db->insert('goods_cat',$data) == 1){
            return (int)$this->db->insertId();
        }
        return -1;
    }

    //修改类别
    public function modify($id,$name,$pid,$point,$level=null,$icon=null,$url=null){
        $id=(int)$id;
        $data['name']=$name;
        $data['pid']=(int)$pid;
        $data['point']=(int)$point;
        $data['level']=$level;
        $data['icon']=$icon;
        $data['url']=$url;
        $data['site_id']=$this->siteId;
        return $this->db->update('goods_cat',$id,$data)==1;
    }
    //检查类别名
    public function checkExist($name,$level,$pid=null){
        $name=$this->db->quote($name);
        $level=$this->db->quote($level);
        if(!empty($pid)){
            $condition = " and `pid`=".(int)$pid;
        }else{
            $condition = '';
        }
        $sql="SELECT 1 FROM `goods_cat` WHERE `name`={$name} and `level`={$level} and site_id=? ".$condition;
        return $this->db->getExist($sql, $this->siteId);
    }
    //获取此类别的信息
    public function getDetail($ids){
        if(empty($ids)) return null;
        $sql = "select * from `goods_cat` where site_id=? and ".$this->db->getIdCondition($ids,'id');
        $r=$this->db->getAll($sql, $this->siteId);
        return is_array($ids) ? $r: reset($r);
    }
    //由父类别ID获取所有子类别
    public function getSon($id){
        $sql = "select * from `goods_cat` where `pid`=? and site_id=?";
        return $this->db->getAll($sql,$id, $this->siteId);
    }

    //获取id的所有子代，包括自己,一维数组显示
    function getPosterity($id){
        $data=array($id);
        for($i=0;$i<count($data);$i++){
            $dataInfo=$this->getSon($data[$i]);
            foreach($dataInfo as $v){
                $data[]=$v['id'];
            }
        }
        return $data;
    }
       //由类别ID获取父类别
    public function getFather($id){
        $data = $this->getDetail($id);
        $sql = "select * from `goods_cat` where id=? and site_id=?";
        return $this->db->getOne($sql,$data['pid'], $this->siteId);
    }
    // 获取ID所有的祖先节点(包括自身id)
    public function getParents($id){
        $parents=array();
        do{
            $data=$this->getDetail($id);
            array_unshift($parents,$data);
            $id=$data['pid'];
        }while($id!=0);
        return $parents;
    }

    //所有商品分类，并且按照树组合好
    public function getAll($pid=0){
        $data=$this->getSon($pid);
        foreach(new ArrayIterator($data) as $k=>$v) {
            $data[$k]['children']=$this->getAll((int)$v['id']);
        }
        return $data;
    }
    /**
     * 获取分类的所有底层分类，用于查询高等级id
     */
    public function getBottom($id=0){
        $origin = $this->getAll($id);
        if(empty($origin)){
            return $id;
        }
        $new = array();
        $ifBottom = 1; //记录原始数据中是否还有下一层分类，初始为有
        while($ifBottom==1){
            $ifBottom = 0;
            foreach ($origin as $key => $value) {
                if(empty($value["children"])){
                    array_push($new, $value['id']);
                    unset($origin[$key]);
                } else {
                    foreach ($value["children"] as $subKey => $subValue) {
                        array_push($origin, $subValue);
                    }
                    unset($origin[$key]);
                    $ifBottom = 1;
                }
            }
        }
        return $new;
    }
     //删除类别Related
    public function deleteAlone($id){
        $id=(int)$id;
        return $this->db->delete('goods_cat',$id)==1;
    }

    //删除类别，包括子类别
    public function deleteRelated($pid){
        $pid=(int)$pid;
        $sql="SELECT `id` FROM `goods_cat` WHERE `pid`=? and site_id=?";
        $ids_arr=$this->db->getAll($sql, $pid, $this->siteId);
        if(!empty($ids_arr)){
            $ids=array_map(function($v){return $v['id'];},$ids_arr);
            $str=implode(",",$ids);
            $str.=",".$pid;
        }else{
            $str=$pid;
        }
        $sql2="DELETE FROM `goods_cat` WHERE `id` IN($str)";
        return $this->db->sqlExec($sql2)>0;
    }
} 